package com.scap.authen.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import com.scap.authen.entity.MenuMapping;
import com.scap.authen.entity.RoleApp;

@Repository("voMenuMappingDao")
public class MenuMappingDaoImpl extends AbstractDao implements MenuMappingDao{

	@Override
	public boolean insertMenuMapping(MenuMapping voMenuMapping) {
		boolean result = false;
		try {
			persist(voMenuMapping);
			result = true;
		} catch (Exception e) {
			result = false;
			e.printStackTrace();
		}
		return result ;
	}

	@Override
	public boolean deleteMenuMapping(String vaSiteId, String vaRoleId, String vaApplicationId) {
		boolean result = false;
		try {
			Query query = getSession().createQuery("Delete from MenuMapping Where vaRoleId = :vaRoleId and vaSiteId = :vaSiteId and vaApplicationId = :vaApplicationId");
			query.setString("vaRoleId", vaRoleId);
			query.setString("vaSiteId", vaSiteId);
			query.setString("vaApplicationId", vaApplicationId);
			query.executeUpdate();
			result = true;
		} catch (Exception e) {
			result = false;
		}
		return result;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MenuMapping> findMenuChecked(String vaAppId, String roleId, String siteId) {
		Criteria criteria = getSession().createCriteria(MenuMapping.class);
		criteria.add(Restrictions.eq("vaSiteId", siteId));
		criteria.add(Restrictions.eq("vaRoleId", roleId));
		criteria.add(Restrictions.eq("vaApplicationId", vaAppId));
		return criteria.list();
	}	
}
